package MyGO.controller.MyController;

import MyGO.Data.GlobalData;
import MyGO.Main;
import MyGO.controller.CheckResultViewController;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

public class PushWindow {
    public static <T> void pushWindow(String fxml, String title, double x, double y, Class<T> controllerClass,
                                      ControllerConfigurator<T> configurator) throws IOException {
        Stage stage = new Stage();

        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(GlobalData.stage);

        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource(fxml));
        AnchorPane anchorPane = fxmlLoader.load();

        T controller = fxmlLoader.getController();
        configurator.configure(controller, stage);

        Scene scene = new Scene(anchorPane, x, y);

        stage.setScene(scene);
        stage.setTitle(title);
        stage.showAndWait();
    }

    @FunctionalInterface
    public interface ControllerConfigurator<T> {
        void configure(T controller, Stage stage);
    }

    public static void pushOnlyPrompt(String fxml, String title, double x, double y) throws IOException {  // 输出提示窗口
        Stage stage = new Stage();

        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(GlobalData.stage);

        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource(fxml));
        AnchorPane anchorPane = fxmlLoader.load();
        Label label = (Label) anchorPane.lookup("#prompt");
        label.setText(title);

        Scene scene = new Scene(anchorPane, x, y);

        stage.setScene(scene);
        stage.showAndWait();
    }

}
